package com.hm.system.service.mongo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import com.hm.common.common.CommonUtil;
import com.hm.common.exception.ServiceException;
import com.hm.common.exception.ErrorCode;
import com.hm.common.model.PageInfo;
import com.hm.system.mongo.domain.system.QSysRoleInfo;
import com.hm.system.mongo.domain.system.SysRoleInfo;
import com.hm.system.mongo.vo.system.SysRoleInfoVo;
import com.hm.system.share.repository.mongo.system.SysRoleInfoRepository;
import com.hm.system.share.service.mongo.system.SysRoleInfoService;
import com.mysema.query.types.expr.BooleanExpression;

/**
 * @author shishun.wang
 * @date 下午2:14:43 2016年5月17日
 * @version 1.0
 * @describe
 */
@Service
@SuppressWarnings({ "rawtypes", "unchecked" })
public class SysRoleInfoServiceImpl implements SysRoleInfoService {

	@Autowired
	private SysRoleInfoRepository sysRoleInfoRepository;
	
	private SysRoleInfo getSysRoleInfoByName(String name){
		return sysRoleInfoRepository.findOne(QSysRoleInfo.sysRoleInfo.name.eq(name));
	}

	@Override
	public String addSysRoleInfo(SysRoleInfoVo sysRoleInfo, String operationUser) {

		if(null != getSysRoleInfoByName(sysRoleInfo.getName())){
			throw ServiceException.warn("角色名称不允许重复");
		}
		
		SysRoleInfo roleInfo = new SysRoleInfo();
		{
			roleInfo.setCreateUser(operationUser);
			roleInfo.setName(sysRoleInfo.getName());
			roleInfo.setNote(sysRoleInfo.getNote());
		}
		return sysRoleInfoRepository.save(roleInfo).getId();
	}

	@Override
	public String updateSysRoleInfo(final SysRoleInfoVo sysRoleInfoVo, String operationUser) {
		
		SysRoleInfo roleInfo = getSysRoleInfoByName(sysRoleInfoVo.getName());
		SysRoleInfo sysRoleInfo = sysRoleInfoRepository.findOne(sysRoleInfoVo.getId());
		if(null != roleInfo){
			if(!roleInfo.getId().equals(sysRoleInfo.getId())){
				throw ServiceException.warn("角色名称不允许重复");
			}
		}
		
		sysRoleInfo = sysRoleInfoRepository.findOne(sysRoleInfoVo.getId());
		{
			sysRoleInfo.setName(sysRoleInfoVo.getName());
			sysRoleInfo.setNote(sysRoleInfoVo.getNote());
		}

		return sysRoleInfoRepository.save(sysRoleInfo).getId();
	}

	@Override
	public String deleteSysRoleInfo(SysRoleInfoVo sysRoleInfo, String operationUser) {
		sysRoleInfoRepository.delete(sysRoleInfo.getId());
		return sysRoleInfo.getId();
	}

	@Override
	public SysRoleInfoVo getSysRoleInfo(String id) {
		SysRoleInfo sysRoleInfo = sysRoleInfoRepository.findOne(id);

		SysRoleInfoVo sysRoleInfoVo = new SysRoleInfoVo();
		if (null == sysRoleInfo) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND, "SysRoleInfoVo:" + id);
		} else {
			sysRoleInfoVo.setId(sysRoleInfo.getId());
			sysRoleInfoVo.setName(sysRoleInfo.getName());
			sysRoleInfoVo.setNote(sysRoleInfo.getNote());
			sysRoleInfoVo.setMenuIds(sysRoleInfo.getMenuIds());
			sysRoleInfoVo.setCreateTime(sysRoleInfo.getCreateTime());
		}
		return sysRoleInfoVo;
	}

	@Override
	public PageInfo<SysRoleInfoVo> querySysRoleInfo(String name, Long startTime, Long endTime, Integer pageNumber,
			Integer pageSize) {
		PageInfo<SysRoleInfoVo> pageInfo = new PageInfo<SysRoleInfoVo>(pageNumber, pageSize);

		QSysRoleInfo qSysRoleInfo = QSysRoleInfo.sysRoleInfo;
		Pageable pageable = new QPageRequest(pageInfo.getPageNumber(), pageInfo.getPageSize(),
				qSysRoleInfo.createTime.desc());

		BooleanExpression predicate = null;
		if(CommonUtil.isNotEmpty(name)){
			predicate = qSysRoleInfo.name.like(name);
		}

		List<SysRoleInfoVo> sysRoleInfoVos = new ArrayList<SysRoleInfoVo>();

		Page page = sysRoleInfoRepository.findAll(predicate, pageable);

		List<SysRoleInfo> content = page.getContent();
		for (SysRoleInfo sysRoleInfo : content) {
			SysRoleInfoVo sysRoleInfoVo = new SysRoleInfoVo();
			{
				sysRoleInfoVo.setId(sysRoleInfo.getId());
				sysRoleInfoVo.setName(sysRoleInfo.getName());
				sysRoleInfoVo.setNote(sysRoleInfo.getNote());
				sysRoleInfoVo.setMenuIds(sysRoleInfo.getMenuIds());
				sysRoleInfoVo.setCreateTime(sysRoleInfo.getCreateTime());
			}

			sysRoleInfoVos.add(sysRoleInfoVo);
		}

		return pageInfo.execute(page, sysRoleInfoVos);
	}

}
